import 'dart:io';

import 'package:aliyun_oss_flutter2/aliyun_oss_flutter2.dart';
import 'package:flutter/material.dart';

import 'image_util.dart';
import 'upload_oss.dart';

class Aliyun2Page extends StatefulWidget {

  @override
  _Aliyun2PageState createState() => _Aliyun2PageState();
}

class _Aliyun2PageState extends State<Aliyun2Page> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
        title: Text('上传'),
    ),
    body:  Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      ListTile(
        title: Text('初始化'),
        onTap: (){
          // 初始化OSSClient
          OSSClient.init(
            endpoint: 'oss-cn-shanghai.aliyuncs.com',
            bucket: 'welfare-game',
            credentials: () async {
              // Future Credentials
              // return Credentials.fromJson(response.data);
              // Or Credentials Config
              return await Credentials(
                accessKeyId: 'STS.NV11ztpZ5J7bMWHvJt6sFS9so',
                accessKeySecret: 'DCbRT4vvqJTsPsMucGs7ftSyqH7SvmV8zJcms9CUh9V1',
                securityToken: 'CAISwQJ1q6Ft5B2yfSjIr5WEesDAnYUU/fWJT3H5kkohOvxqvPzYjTz2IHpEf3BhAeAWsf00mGxR6vsclq8qE8MaGxCczgyDKA4Qo22beIPkl5GfD9Vm4cTX9QHxZjf/2MjNGaqbKPrWZvaqbX3diyZ32sGUXD6+XlujQ/rr7Jl8dYYvQxWfZzhLD8ssHGkEksIBMmbLPvuAKwPjhnGqbHBloQ1hk2hym/rdsMSX8UjZl0aoiL1X9Zv8JpatdcQpM556XpD3vpx/fbGT1zVLuVoYtvVpgahK8C3K+96UBlRzm0zYb7WJr4Y+cFUiNvlqQ/Z+waKixaEiiIv6jJ/qzhtBB+ZRXhnESZqoqMm+Q7n5bY9oLearZC2WjozWasSrqXAtZXMKKQVNYME9LXx9DxMhTDzAI7Og4krNZgqzi0gdY1mQT/AagAFJYpb7ow2zENv1mQqIk6asGuzZq4/dVNUOr4QVZFXTPOsAsjDAd0AZC0qNmVsti7qkZKLJwM1scOUia4ZkCGSVi1XSKPq95JRNe1SNR997XpPmsv5BKQrMdUUP76qqjRqZnbsVugjpz+7JQElW869GGuQqfkjF2SjWTjIqrKcEyw==',
                // expiration: '',
              );
            },
          );
        },
      ),

      ListTile(
        title: Text('上传'),
        onTap: ()async {
          String filePath = await ImageUtil.selectImage();
          final object = await OSSClient().putObject(
            OSSObjectPut.fromFile(file: File(filePath), key: '/article/image/head.png'),
            bucket: 'welfare-game', // String?
            endpoint: 'oss-cn-shanghai.aliyuncs.com', // String?
            path: '/article/image', // String?
          );
        },
      ),


      ListTile(
        title: Text('上传2'),
        onTap: ()async {
          String filePath = await ImageUtil.selectImage();
          final String url = await UploadOss.upload(file: File(filePath));
          print('上传成功');
          print(url);
        },
      ),




    ],)
    );
  }

}